package cn.com.cetccst.appstore.jssdk;

import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.ViewModelProvider;

import com.amap.api.location.AMapLocation;
import com.google.gson.annotations.SerializedName;

import cn.com.cetccst.appstore.jssdk.viewmodel.JSSDKViewModel;
import cn.com.cetccst.appstore.tools.Utils;
import cn.com.cetccst.sdk.modelbase.BaseResp;


public class ActionRegisterListener extends BaseJSAction<ActionRegisterListener.RequestPara, AMapLocation> {
//    private int errorCode = -1;
    private FragmentActivity mActivity;
    private JSSDKViewModel viewModel;

    ActionRegisterListener(FragmentActivity context, CXApiImplInnerApp icxapi, String parameters) {
        super(context, icxapi, parameters);
        this.mActivity = context;
        this.viewModel = new ViewModelProvider(mActivity).get(JSSDKViewModel.class);
    }

    @Override
    public void handleAction(Response callback) {
        if (!checkBaseArgs(callback)) {
            return;
        }
        handleInner(callback);
    }

    private void handleInner(Response callback) {
        if (viewModel.addListener(para.listener, para.listenerCallBackName)) {
            callback.setRespJson(buildSuccessResp(null));
        } else {
            callback.setRespJson(buildFailedResp(BaseResp.ErrCode.ERROR_REGISTER_FAILED));
        }
    }

    @Override
    protected RequestPara parserParameters(String para) {
        return Utils.getGson().fromJson(para, RequestPara.class);
    }

    @Override
    protected String buildSuccessResp(AMapLocation result) {
        Resp<RespData> resp = new Resp();
        resp.code = 0;
        resp.data = new RespData();
        return Utils.getGson().toJson(resp);
    }

    static class RequestPara {
        @SerializedName("listener")
        String listener;
        @SerializedName("listenerCallBackName")
        String listenerCallBackName;
    }

    static class RespData {

    }
}
